<?php

use \Yosymfony\Toml\Toml;

use globalunit\utils\Utils;
use QYS\QYS;

$gameroot = dirname(getcwd());
// var_dump($gameroot);

function joinPaths()
{
    $args = func_get_args();

    $paths = [];

    foreach ($args as $arg) {
        $paths[] = trim($arg, DIRECTORY_SEPARATOR);
    }

    $paths = array_filter($paths);

    return DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, $paths);
}

function curl_get($url)
{
    $curl = curl_init();
    $header = array(
        'Accept: application/json',
    );

    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 超时设置,以秒为单位
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);

    // 超时设置，以毫秒为单位
    // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    //执行命令
    $data = curl_exec($curl);

    // 显示错误信息
    if (curl_error($curl)) {
        print "Error: " . curl_error($curl);
    } else {
        // 打印返回的内容
        // var_dump($data);
        // curl_close($curl);
        return $data;
    }
}
$config = [];
$path = joinPaths(QYS::getProjPath(), "../..", "config", "dev.toml");
if(file_exists($path)){
    // var_dump($path);
    $myfile = fopen($path, "r");
    $content = fread($myfile, filesize($path));
    $config = Toml::Parse($content);
}else{
    $apiip = getenv('IP');
    $apiport = getenv('PORT');
    $url = $apiip . ":" . $apiport . "/configcenter/config";
    // var_dump($url);
    $config = curl_get($url);
    $config = json_decode($config, true);
    $config = $config['data']['config'];
}
// var_dump($config);
return array(
    "pack_path" => array("callback", 'lib', 'scripts', "handler", ".."),
    'server_mode' => 'Process',
    'process' => array(
        "worker_num" => 4,
        'callback_class' => 'callback\\SwooleLogProcess',
    ),
    'LOG_HANDLER' => 0,
    'GAME_HANDLER' => 1,
    "debug" => array(
        "Test" => true,
    ),
    'gamename' => $config["global"]["gamename"],
    'version' => $config["global"]["version"],
    'ip' => $config["server"]["ip"],
    'port' => $config["server"]["port"],
    'mongo' => array(
        "mongo1" => array(
            'host' => $config["mongo"]["ip"],
            'port' => $config["mongo"]["port"],
        ),
    ),
    'mysql' => array(
        "mysql1" => array(
            'host' => $config["mysql"]["ip"],
            'user' => $config["mysql"]["user"],
            'password' => $config["mysql"]["password"],
            'database' => $config["mysql"]["db"],
            'port' => $config["mysql"]["port"],
        ),
    ),
    'redis' => array(
        "redis1" => array(
            'host' => $config["redis"]["ip"],
            'port' => $config["redis"]["port"],
            'hearbeat' => true,
        ),
        "queue" => array(
            'host' => $config["queue"]["ip"],
            'port' => $config["queue"]["port"],
            'hearbeat' => true,
        ),
    ),
);
